using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using Toenda.TFL.Base;
using Toenda.Rails.Data;
using Toenda.Rails.System;
using Toenda.Rails.DataContainer;

public partial class Controls_Main_ctrl_imageview : System.Web.UI.UserControl {
	private AccountProvider ap = new AccountProvider();
	private ImageProvider ip = new ImageProvider();
	private ImageItem item = new ImageItem();
	private Account acc = new Account();
	private Account tmp = new Account();
	private ServerPath serverpath = new ServerPath();

	/// <summary>
	/// Page_Load
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e) {
		bool letView = false;
		bool isLoggedIn = false;
		bool isOwner = false;
		//bool isInSet = false;
		bool showImage = true;

		if(ip.CheckImageExists(Request.QueryString.Get("item"))) {
			//if(Session.Count > 0 && ap.CheckSessionExist(Session["Zappr"].ToString())) {
			if(Session.Count > 0 && Session["ZapprUserId"] != null) {
				tmp = ap.GetAccount(Session["ZapprUserId"].ToString());
				isLoggedIn = true;
			}

			if(Request.QueryString.Get("item") != null) {
				this.btnAllSizes.PostBackUrl += "?item=" + Request.QueryString.Get("item");

				this.btnAddToSet.PostBackUrl += "default.aspx?id=_addtosetRedirect&photo=" + Request.QueryString.Get("Item");
				//this.btnAddToSet.PostBackUrl += "_addtoset/photo/" + Request.QueryString.Get("Item") + "/default.aspx";

				this.btnEdit.PostBackUrl += "default.aspx?id=_edit&photo=" + Request.QueryString.Get("Item");
				//this.btnEdit.PostBackUrl += "_edit/photo/" + Request.QueryString.Get("Item") + "/default.aspx";
				
				this.btnDelete.PostBackUrl += "";

				item = ip.GetImage(Convert.ToInt64(Request.QueryString.Get("item")));
				acc = ap.GetAccount(item.UserID);

				// save comment
				if(isLoggedIn && item.UserID == tmp.ID) {
				}

				// check privacy
				if(isLoggedIn && item.UserID == tmp.ID) {
					if(item.Access > 0) {
						// this time: only the user itself
						// next time: friends and family
						if(item.UserID == acc.ID) {
							letView = true;
						}
					}
					else {
						letView = true;
					}
				}
				else {
					if(item.Access == 0) {
						letView = true;
					}
				}

				/*
				 * image
				 */

				if(letView) {
					// count image views
					if(!IsPostBack) {
						ip.CountImageView(item.ID);
					}

					// image
					System.Drawing.Bitmap img = new System.Drawing.Bitmap(
						this.serverpath.Path + "\\Images\\" + item.Image500
					);

					if(img.Width < 500) {
						imgView.CssClass = "imgView2";
					}

					imgView.ImageUrl = "~/Images/" + item.Image500;

					/*
					 * text
					 */

					// text
					if(item.Description != "") {
						Label text = new Label();
						text.CssClass = "listText";
						text.Text = item.Description;

						imgInfo.Controls.Add(text);

						imgInfoBreaks.Visible = true;
					}

					/*
					 * comments
					 */

					// comments title
					int comments_amount = ip.GetImageCommentAmount(item.ID);

					if(comments_amount > 0) {
						lblCommentsTitle.Text = GetLocalResourceObject("Comments").ToString();
						imgBreaks.Visible = true;
					}
					else {
						if(isLoggedIn) {
							lblCommentsTitle.Text = GetLocalResourceObject("AddAComment").ToString();
							imgBreaks.Visible = true;
						}
					}

					// write comment
					if(isLoggedIn) {
						imgAddComment.Visible = true;
						imgWouldComment.Visible = false;
					}
					else {
						imgAddComment.Visible = false;
						imgWouldComment.Visible = true;
					}

					// comments
					ArrayList arr = ip.GetImageCommentList(item.ID);

					Table tbl = new Table();
					tbl.BorderWidth = new Unit(0);
					tbl.CellPadding = 5;
					tbl.CellSpacing = 0;

					foreach(CommentItem cmt in arr) {
						TableRow row = new TableRow();

						// br
						TableCell cell0 = new TableCell();
						cell0.ColumnSpan = 2;
						cell0.Controls.Add(new LiteralControl("<br />"));

						row.Cells.Add(cell0);
						tbl.Rows.Add(row);

						row = null;

						//
						// cell1
						//
						TableCell cell1 = new TableCell();
						cell1.VerticalAlign = VerticalAlign.Top;

						// avatar
						HyperLink av = new HyperLink();
						av.CssClass = "avatarImg";
						//av.NavigateUrl = "~/default.aspx?id=images&user=" + cmt.Author.Username;
						av.NavigateUrl = "~/images/" + cmt.Author.Username + "/default.aspx";

						if(cmt.Author.Avatar != "" && cmt.Author.Avatar != null) {
							av.ImageUrl = "~/Avatar/" + cmt.Author.Avatar;
						}
						else {
							av.ImageUrl = "~/App_Themes/Default/Images/avatar_placeholder.jpg";
						}

						cell1.Controls.Add(av);

						//
						// cell2
						//
						TableCell cell2 = new TableCell();
						cell2.VerticalAlign = VerticalAlign.Top;

						// comment author
						HyperLink author = new HyperLink();
						author.CssClass = "commentAuthor";
						author.Text = cmt.Author.Username;
						//author.NavigateUrl = "~/default.aspx?id=images&user=" + cmt.Author.Username;
						author.NavigateUrl = "~/images/" + cmt.Author.Username + "/default.aspx";

						cell2.Controls.Add(author);

						// comment author extra
						Label author2 = new Label();
						author2.CssClass = "commentAuthorExtra";
						author2.Text = " " + GetLocalResourceObject("HaveToSay").ToString();

						cell2.Controls.Add(author2);

						// comment text
						Label comment = new Label();
						comment.CssClass = "commentText";
						comment.Text = "<br /><br />";
						comment.Text += Converter.ConvertLinebreak(cmt.Comment);
						comment.Text += "<br />";

						cell2.Controls.Add(comment);

						// add to table
						row = new TableRow();
						row.Cells.Add(cell1);
						row.Cells.Add(cell2);
						tbl.Rows.Add(row);
					}

					imgComments.Controls.Add(tbl);

					/*
					 * actions
					 */

					// add to favs
					if(isLoggedIn) {
						if(ip.ImageIsFavorite(tmp.ID, item.ID)) {
							btnFavo.CssClass = "btnActionFavOn";
							btnFavo.Text = GetLocalResourceObject("IsAFav").ToString();
						}
					}
					else {
						btnFavo.Visible = false;
					}

					// all sizes
					if(item.Licence == "cl") {
						showImage = false;

						if(item.UserID == tmp.ID) {
							showImage = true;
						}
					}
					else {
						showImage = true;
					}

					if(!showImage) {
						btnAllSizes.Visible = false;
					}

					// add to set & edit
					if(isLoggedIn && item.UserID == tmp.ID) {
						btnAddToSet.Visible = true;
						btnEdit.Visible = true;
					}
					else {
						btnAddToSet.Visible = false;
						btnEdit.Visible = false;
					}

					// delete
					if((isLoggedIn && item.UserID == tmp.ID)
					|| (tmp.Type == 2)) {
						btnDelete.Visible = true;
					}
					else {
						btnDelete.Visible = false;
					}

					Literal litX = new Literal();
					litX.Text = "<div style=\"display: block; float: none; height: 6px;\">";
					litX.Text += "</div>";

					imgActions.Controls.Add(litX);
				}
				else {
					Label priv = new Label();
					priv.CssClass = "listText";
					priv.Text = GetLocalResourceObject("ImageIsPrivate").ToString();
					priv.Text += "<br />";
					priv.Text += "<br />";

					imgInfo.Controls.Add(priv);

					imgAddComment.Visible = false;
				}
			}
		}
		else {
			imgInfoBreaks.Visible = false;
			imgComments.Visible = false;
			imgBreaks.Visible = false;
			imgAddComment.Visible = false;
			imgWouldComment.Visible = false;

			Label not = new Label();
			not.CssClass = "txtNormal";
			not.Text = GetLocalResourceObject("ImgDoesNotExists").ToString();

			imgInfo.Controls.Add(not);
		}
	}

	/// <summary>
	/// Save event
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnSaveNow_Click(object sender, EventArgs e) {
		if(txtComment.Text.Trim() == "") {
			return;
		}
		
		ImageItem image = ip.GetImage(Convert.ToInt64(Request.QueryString.Get("item")));

		CommentItem item = new CommentItem();
		item.ID = Guid.NewGuid().ToString();
		item.Author = ap.GetAccount(Session["ZapprUserId"].ToString());
		item.ImageID = image.ID;
		item.Date = DateTime.Now;
		item.Comment = txtComment.Text.Trim();

		ip.SaveComment(item);

		//Response.Redirect("~/default.aspx?item=" + Request.QueryString.Get("item"));
		Response.Redirect("~/photo/" + Request.QueryString.Get("item") + "/default.aspx");
	}

	/// <summary>
	/// add to favorites
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnFav_Click(object sender, EventArgs e) {
		if(ip.ImageIsFavorite(tmp.ID, item.ID)) {
			btnFavo.CssClass = "btnActionFav";
			btnFavo.Text = GetLocalResourceObject("AddToFavs").ToString();
			ip.DeleteFavorite(tmp.ID, item.ID);
		}
		else {
			btnFavo.CssClass = "btnActionFavOn";
			btnFavo.Text = GetLocalResourceObject("IsAFav").ToString();
			ip.SaveFavoriteImage(tmp.ID, item.ID);
		}
	}
}
